import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.List;

import org.apache.poi.hslf.usermodel.HSLFSlide;
import org.apache.poi.hslf.usermodel.HSLFSlideShow;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.xslf.usermodel.XSLFSlide;


public class ppt
{
	//源路径/目标路径/要转换的页数/生成的图片的宽度（像素）/图片之间的间距（像素）/横向几张图片
	//src dest page wide interval columns
	public static void main(String[] argv) throws Exception
	{
		String src = argv[0];
		String dest = "C:\\Users\\THORQQ\\Desktop\\ppt-img";
		int page = 13;  //截图13页
		int wide = 750; //图片宽度750像素
		int space = 2;  //小图间距2个像素
		int horizontal = 3;  //水平放置3个页面
		
		if(src.endsWith("pptx"))
		{
			pptx2img(src);
		}
		else if(src.endsWith("ppt"))
		{
			ppt2img(src);
		}
		else
		{
			System.out.println("Unknown file: " + src);
		}
	}
	
	public static void ppt2img(String file) throws Exception
	{
		FileInputStream is = new FileInputStream(file);
	    HSLFSlideShow ppt = new HSLFSlideShow(is);
	    is.close();

	    Dimension pgsize = ppt.getPageSize();

	    int idx = 1;
	    for (HSLFSlide slide : ppt.getSlides()) {

	        BufferedImage img = new BufferedImage(pgsize.width, pgsize.height, BufferedImage.TYPE_INT_RGB);
	        Graphics2D graphics = img.createGraphics();
	        // clear the drawing area
	        graphics.setPaint(Color.white);
	        graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));

	        // render
	        slide.draw(graphics);

	        // save the output
	        FileOutputStream out = new FileOutputStream("slide-" + idx + ".jpg");
	        javax.imageio.ImageIO.write(img, "jpg", out);
	        out.close();

	        idx++;
	    }
	    
	    ppt.close();
	}
	
	public static void pptx2img(String file) throws Exception
	{
		// read the .pptx file
        XMLSlideShow ppt = new XMLSlideShow(OPCPackage.open(file));

	    Dimension pgsize = ppt.getPageSize();

        List<XSLFSlide> slide = ppt.getSlides();
        for (int i = 0; i < slide.size(); i++) 
        {
	        BufferedImage img = new BufferedImage(pgsize.width, pgsize.height, BufferedImage.TYPE_INT_RGB);
	        Graphics2D graphics = img.createGraphics();
	        // clear the drawing area
	        graphics.setPaint(Color.white);
	        graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));


            // draw stuff. All the heavy-lifting happens here
            slide.get(i).draw(graphics);

            // save the result.
	        FileOutputStream out = new FileOutputStream("slide-" + (i+1) + ".jpg");
	        javax.imageio.ImageIO.write(img, "jpg", out);
	        out.close();


        }
        
        ppt.close();
	}
}